﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json.Linq;

namespace Service
{
    public class DataCollector : IDataService
    {
        #region IDataService Members

        public void InsertAchievements(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string) jsonParser.SelectToken("characterId");
            var completedAchievements = (string) jsonParser.SelectToken("completedAchievements");
            var completedTimeStamps = (string) jsonParser.SelectToken("completedTimeStamps");

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlCommand = new SqlCommand {CommandType = CommandType.StoredProcedure, Connection = sqlConnection, CommandText = "InsertCharacterAchievements"};
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
            sqlCommand.Parameters.AddWithValue("AchievementsCompleted", completedAchievements);
            sqlCommand.Parameters.AddWithValue("AchievementsCompletedTimestamp", completedTimeStamps);
            sqlCommand.ExecuteScalar();

            sqlConnection.Close();
        }

        public void InsertAppearance(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = int.Parse(jsonParser.SelectToken("characterId").ToString());
            var faceVariation = int.Parse(jsonParser.SelectToken("faceVariation").ToString());
            var featureVariation = int.Parse(jsonParser.SelectToken("featureVariation").ToString());
            var hairColor = int.Parse(jsonParser.SelectToken("hairColor").ToString());
            var hairVariation = int.Parse(jsonParser.SelectToken("hairVariation").ToString());
            var showCloak = bool.Parse(jsonParser.SelectToken("showCloak").ToString());
            var showHelm = bool.Parse(jsonParser.SelectToken("showHelm").ToString());
            var skinColor = int.Parse(jsonParser.SelectToken("skinColor").ToString());

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlCommand = new SqlCommand {CommandType = CommandType.StoredProcedure, Connection = sqlConnection, CommandText = "InsertCharacterAppearance"};
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
            sqlCommand.Parameters.AddWithValue("FaceVariation", faceVariation);
            sqlCommand.Parameters.AddWithValue("FeatureVariation", featureVariation);
            sqlCommand.Parameters.AddWithValue("HairColor", hairColor);
            sqlCommand.Parameters.AddWithValue("HairVariation", hairVariation);
            sqlCommand.Parameters.AddWithValue("ShowCloak", showCloak);
            sqlCommand.Parameters.AddWithValue("ShowHelm", showHelm);
            sqlCommand.Parameters.AddWithValue("SkinColor", skinColor);
            sqlCommand.ExecuteScalar();

            sqlConnection.Close();
        }

        public void InsertCompanions(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string)jsonParser.SelectToken("characterId");
            var companionsAsString = (string)jsonParser.SelectToken("companionsAsString");

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlCommand = new SqlCommand { CommandType = CommandType.StoredProcedure, Connection = sqlConnection, CommandText = "InsertCharacterCompanions" };
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
            sqlCommand.Parameters.AddWithValue("Companions", companionsAsString);
            sqlCommand.ExecuteScalar();

            sqlConnection.Close();
        }

        public void InsertItems(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            InsertItem(jsonParser, "back", sqlConnection);
            InsertItem(jsonParser, "chest", sqlConnection);
            InsertItem(jsonParser, "feet", sqlConnection);
            InsertItem(jsonParser, "finger1", sqlConnection);
            InsertItem(jsonParser, "finger2", sqlConnection);
            InsertItem(jsonParser, "hands", sqlConnection);
            InsertItem(jsonParser, "head", sqlConnection);
            InsertItem(jsonParser, "legs", sqlConnection);
            InsertItem(jsonParser, "mainHand", sqlConnection);
            InsertItem(jsonParser, "neck", sqlConnection);
            InsertItem(jsonParser, "offHand", sqlConnection);
            InsertItem(jsonParser, "ranged", sqlConnection);
            InsertItem(jsonParser, "shirt", sqlConnection);
            InsertItem(jsonParser, "shoulder", sqlConnection);
            InsertItem(jsonParser, "tabard", sqlConnection);
            InsertItem(jsonParser, "trinket1", sqlConnection);
            InsertItem(jsonParser, "trinket2", sqlConnection);
            InsertItem(jsonParser, "waist", sqlConnection);
            InsertItem(jsonParser, "wrist", sqlConnection);

            sqlConnection.Close();
        }

        public void InsertItemEnhancements(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));
            var characterId = (string) jsonParser.SelectToken("characterId");

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            InsertItemEnhancement(characterId, jsonParser, "back", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "chest", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "feet", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "finger1", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "finger2", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "hands", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "head", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "legs", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "mainHand", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "neck", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "offHand", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "ranged", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "shirt", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "shoulder", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "tabard", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "trinket1", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "trinket2", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "waist", sqlConnection);
            InsertItemEnhancement(characterId, jsonParser, "wrist", sqlConnection);

            sqlConnection.Close();
        }

        public void InsertEquipment(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string) jsonParser.SelectToken("characterId");
            var averageItemLevel = (string) jsonParser.SelectToken("averageItemLevel");
            var averageItemLevelEquipped = (string) jsonParser.SelectToken("averageItemLevelEquipped");
            var backItemId = (string) jsonParser.SelectToken("backItemId");
            var chestItemId = (string) jsonParser.SelectToken("chestItemId");
            var feetItemId = (string) jsonParser.SelectToken("feetItemId");
            var finger1ItemId = (string) jsonParser.SelectToken("finger1ItemId");
            var finger2ItemId = (string) jsonParser.SelectToken("finger2ItemId");
            var handsItemId = (string) jsonParser.SelectToken("handsItemId");
            var headItemId = (string) jsonParser.SelectToken("headItemId");
            var legsItemId = (string) jsonParser.SelectToken("legsItemId");
            var mainHandItemId = (string) jsonParser.SelectToken("mainHandItemId");
            var neckItemId = (string) jsonParser.SelectToken("neckItemId");
            var offHandItemId = (string) jsonParser.SelectToken("offHandItemId");
            var rangedItemId = (string) jsonParser.SelectToken("rangedItemId");
            var shirtItemId = (string) jsonParser.SelectToken("shirtItemId");
            var shoulderItemId = (string) jsonParser.SelectToken("shoulderItemId");
            var tabardItemId = (string) jsonParser.SelectToken("tabardItemId");
            var trinket1ItemId = (string) jsonParser.SelectToken("trinket1ItemId");
            var trinket2ItemId = (string) jsonParser.SelectToken("trinket2ItemId");
            var waistItemId = (string) jsonParser.SelectToken("waistItemId");
            var wristItemId = (string) jsonParser.SelectToken("wristItemId");

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlCommand = new SqlCommand {CommandType = CommandType.StoredProcedure, Connection = sqlConnection, CommandText = "InsertCharacterEquipment"};
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
            sqlCommand.Parameters.AddWithValue("AverageItemLevel", averageItemLevel);
            sqlCommand.Parameters.AddWithValue("AverageItemLevelEquipped", averageItemLevelEquipped);
            sqlCommand.Parameters.AddWithValue("Back", (string.IsNullOrEmpty(backItemId) ? "" : backItemId));
            sqlCommand.Parameters.AddWithValue("Chest", (string.IsNullOrEmpty(chestItemId) ? "" : chestItemId));
            sqlCommand.Parameters.AddWithValue("Feet", (string.IsNullOrEmpty(feetItemId) ? "" : feetItemId));
            sqlCommand.Parameters.AddWithValue("Finger1", (string.IsNullOrEmpty(finger1ItemId) ? "" : finger1ItemId));
            sqlCommand.Parameters.AddWithValue("Finger2", (string.IsNullOrEmpty(finger2ItemId) ? "" : finger2ItemId));
            sqlCommand.Parameters.AddWithValue("Hands", (string.IsNullOrEmpty(handsItemId) ? "" : handsItemId));
            sqlCommand.Parameters.AddWithValue("Head", (string.IsNullOrEmpty(headItemId) ? "" : headItemId));
            sqlCommand.Parameters.AddWithValue("Legs", (string.IsNullOrEmpty(legsItemId) ? "" : legsItemId));
            sqlCommand.Parameters.AddWithValue("MainHand", (string.IsNullOrEmpty(mainHandItemId) ? "" : mainHandItemId));
            sqlCommand.Parameters.AddWithValue("Neck", (string.IsNullOrEmpty(neckItemId) ? "" : neckItemId));
            sqlCommand.Parameters.AddWithValue("OffHand", (string.IsNullOrEmpty(offHandItemId) ? "" : offHandItemId));
            sqlCommand.Parameters.AddWithValue("Ranged", (string.IsNullOrEmpty(rangedItemId) ? "" : rangedItemId));
            sqlCommand.Parameters.AddWithValue("Shirt", (string.IsNullOrEmpty(shirtItemId) ? "" : shirtItemId));
            sqlCommand.Parameters.AddWithValue("Shoulder", (string.IsNullOrEmpty(shoulderItemId) ? "" : shoulderItemId));
            sqlCommand.Parameters.AddWithValue("Tabard", (string.IsNullOrEmpty(tabardItemId) ? "" : tabardItemId));
            sqlCommand.Parameters.AddWithValue("Trinket1", (string.IsNullOrEmpty(trinket1ItemId) ? "" : trinket1ItemId));
            sqlCommand.Parameters.AddWithValue("Trinket2", (string.IsNullOrEmpty(trinket2ItemId) ? "" : trinket2ItemId));
            sqlCommand.Parameters.AddWithValue("Waist", (string.IsNullOrEmpty(waistItemId) ? "" : waistItemId));
            sqlCommand.Parameters.AddWithValue("Wrist", (string.IsNullOrEmpty(wristItemId) ? "" : wristItemId));
            sqlCommand.ExecuteScalar();

            sqlConnection.Close();
        }

        public void InsertMounts(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string) jsonParser.SelectToken("characterId");
            var mounts = (string)jsonParser.SelectToken("mounts");

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlCommand = new SqlCommand { CommandType = CommandType.StoredProcedure, Connection = sqlConnection, CommandText = "InsertCharacterMounts" };
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
            sqlCommand.Parameters.AddWithValue("Mounts", mounts);

            sqlCommand.ExecuteScalar();
            sqlConnection.Close();
        }

        public void InsertPets(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string)jsonParser.SelectToken("characterId");
            var numberOfPets = int.Parse(jsonParser.SelectToken("numberOfPets").ToString());

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            for(int i = 0; i < numberOfPets; i++)
            {
                var petId = (string)jsonParser.SelectToken("pets[" + i + "].petId");
                var talentBuild = (string)jsonParser.SelectToken("pets[" + i + "].talentBuild");
                var petName = (string)jsonParser.SelectToken("pets[" + i + "].name");

                var sqlCommand = new SqlCommand { CommandType = CommandType.StoredProcedure, Connection = sqlConnection, CommandText = "InsertCharacterPets" };
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("PetId", petId);
                sqlCommand.Parameters.AddWithValue("TalentBuild", talentBuild);
                sqlCommand.Parameters.AddWithValue("Name", petName);
                sqlCommand.ExecuteScalar();
            }

            sqlConnection.Close();
        }

        public void DeleteProfessions(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string)jsonParser.SelectToken("characterId");

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlCommand = new SqlCommand { Connection = sqlConnection, CommandText = "DELETE FROM Character_Profession WHERE CharacterId='" + characterId + "'" };
            sqlCommand.ExecuteNonQuery();

            sqlConnection.Close();
        }

        public void InsertProfessions(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string)jsonParser.SelectToken("characterId");
            var numberOfProfessions = int.Parse(jsonParser.SelectToken("numberOfProfessions").ToString());

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            for (int i = 0; i < numberOfProfessions; i++)
            {
                var name = (string)jsonParser.SelectToken("professions[" + i + "].name");
                var rank = (string)jsonParser.SelectToken("professions[" + i + "].rank");
                var max = (string)jsonParser.SelectToken("professions[" + i + "].max");
                var recipies = (string)jsonParser.SelectToken("professions[" + i + "].recipies");

                var sqlCommand = new SqlCommand {Connection = sqlConnection, CommandType = CommandType.StoredProcedure, CommandText = "InsertCharacter_Profession",};
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("Profession", name);
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("Rank", rank);
                sqlCommand.Parameters.AddWithValue("Max", max);
                sqlCommand.Parameters.AddWithValue("Recipes", recipies);
                sqlCommand.ExecuteScalar();
            }
            sqlConnection.Close();
        }

        public void InsertRaidInstances(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var numberOfInstances = int.Parse(jsonParser.SelectToken("numberOfInstances").ToString());

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            for (int i = 0; i < numberOfInstances; i++)
            {
                var instanceId = (string) jsonParser.SelectToken("instances[" + i + "].instanceId");
                var instanceName = (string)jsonParser.SelectToken("instances[" + i + "].name");

                var sqlCommand = new SqlCommand {Connection = sqlConnection, CommandType = CommandType.StoredProcedure, CommandText = "InsertRaidInstance"};
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("InstanceId", instanceId);
                sqlCommand.Parameters.AddWithValue("Name", instanceName);
                sqlCommand.ExecuteScalar();
            }

            sqlConnection.Close();
        }

        public void InsertProgression(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var numberOfBosses = int.Parse(jsonParser.SelectToken("numberOfBosses").ToString());
            var characterId = (string)jsonParser.SelectToken("characterId");

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            for (int i = 0; i < numberOfBosses; i++)
            {
                var raidBossId = (string) jsonParser.SelectToken("bosses[" + i + "].raidBossId");
                var raidBossName = (string)jsonParser.SelectToken("bosses[" + i + "].raidBossName");
                var instanceId = (string)jsonParser.SelectToken("bosses[" + i + "].instanceId");
                var normalKills = (string)jsonParser.SelectToken("bosses[" + i + "].normalKills");
                var heroicKills = (string)jsonParser.SelectToken("bosses[" + i + "].heroicKills");

                var sqlCommand = new SqlCommand {Connection = sqlConnection, CommandType = CommandType.StoredProcedure, CommandText = "InsertRaidBoss"};
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("RaidBossId", raidBossId);
                sqlCommand.Parameters.AddWithValue("Name", raidBossName);
                sqlCommand.Parameters.AddWithValue("InstanceId", instanceId);
                sqlCommand.ExecuteScalar();

                sqlCommand = new SqlCommand {Connection = sqlConnection, CommandType = CommandType.StoredProcedure, CommandText = "InsertCharacter_Progression"};
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("BossId", raidBossId);
                sqlCommand.Parameters.AddWithValue("NormalKills", normalKills);
                sqlCommand.Parameters.AddWithValue("HeroicKills", heroicKills);
                sqlCommand.ExecuteScalar();
            }

            sqlConnection.Close();
        }

        public void InsertBattlegrounds(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));
            var characterId = (string)jsonParser.SelectToken("characterId");
            var numberOfBattlegrounds = int.Parse(jsonParser.SelectToken("numberOfBattlegrounds").ToString());

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            for (int i = 0; i < numberOfBattlegrounds; i++)
            {
                var battlegroundName = (string) jsonParser.SelectToken("battlegrounds[" + i + "].battlegroundName");
                var played = (string) jsonParser.SelectToken("battlegrounds[" + i + "].played");
                var won = (string) jsonParser.SelectToken("battlegrounds[" + i + "].won");

                var sqlCommand = new SqlCommand {CommandType = CommandType.StoredProcedure, Connection = sqlConnection, CommandText = "InsertCharacterBattleground"};
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("BattlegroundName", battlegroundName);
                sqlCommand.Parameters.AddWithValue("Played", played);
                sqlCommand.Parameters.AddWithValue("Won", won);
                sqlCommand.ExecuteScalar();
            }

            sqlConnection.Close();
        }

        public void DeleteArenaTeams(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string)jsonParser.SelectToken("characterId");

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlDeleteCommand = new SqlCommand {Connection = sqlConnection, CommandType = CommandType.Text, CommandText = "DELETE FROM Character_ArenaTeams WHERE CharacterId='" + characterId + "'"};
            sqlDeleteCommand.ExecuteNonQuery();

            sqlConnection.Close();
        }

        public void InsertArenaTeams(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string)jsonParser.SelectToken("characterId");
            var numberOfArenaTeams = int.Parse(jsonParser.SelectToken("numberOfArenaTeams").ToString());

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            for (int i = 0; i < numberOfArenaTeams; i++)
            {
                var arenaTeamName = (string) jsonParser.SelectToken("arenaTeams[" + i + "].arenaTeamName");
                var arenaTeamSize = (string)jsonParser.SelectToken("arenaTeams[" + i + "].arenaTeamSize");
                var arenaTeamRating = (string)jsonParser.SelectToken("arenaTeams[" + i + "].arenaTeamRating");
                var realmId = (string)jsonParser.SelectToken("arenaTeams[" + i + "].realmId");
                var arenaTeamPersonalRating = (string)jsonParser.SelectToken("arenaTeams[" + i + "].arenaTeamPersonalRating");

                var sqlCommand = new SqlCommand {CommandType = CommandType.StoredProcedure, Connection = sqlConnection, CommandText = "InsertArenaTeam"};
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("Name", arenaTeamName);
                sqlCommand.Parameters.AddWithValue("Size", arenaTeamSize);
                sqlCommand.Parameters.AddWithValue("Rating", arenaTeamRating);
                sqlCommand.Parameters.AddWithValue("RealmId", realmId);
                string arenaId = sqlCommand.ExecuteScalar().ToString();

                sqlCommand.CommandText = "InsertCharacter_ArenaTeam";
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("ArenaId", arenaId);
                sqlCommand.Parameters.AddWithValue("Rating", arenaTeamPersonalRating);
                sqlCommand.ExecuteScalar();
            }

            sqlConnection.Close();
        }

        public void InsertQuests(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string)jsonParser.SelectToken("characterId");
            var completedQuests = (string)jsonParser.SelectToken("completedQuests");

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlCommand = new SqlCommand { CommandType = CommandType.StoredProcedure, Connection = sqlConnection, CommandText = "InsertCharacterQuests" };
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
            sqlCommand.Parameters.AddWithValue("QuestsCompleted", completedQuests);
            sqlCommand.ExecuteScalar();

            sqlConnection.Close();
        }

        public void InsertReputations(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = int.Parse(jsonParser.SelectToken("characterId").ToString());
            var numberOfReputations = int.Parse(jsonParser.SelectToken("numberOfReputations").ToString());

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            for (int i = 0; i < numberOfReputations; i++)
            {
                var factionId = int.Parse(jsonParser.SelectToken("reputations[" + i + "].factionId").ToString());
                var factionName = (string)jsonParser.SelectToken("reputations[" + i + "].factionName");
                var standing = (string)jsonParser.SelectToken("reputations[" + i + "].standing");
                var value = int.Parse(jsonParser.SelectToken("reputations[" + i + "].value").ToString());
                var max = int.Parse(jsonParser.SelectToken("reputations[" + i + "].max").ToString());

                var sqlCommand = new SqlCommand {Connection = sqlConnection, CommandType = CommandType.StoredProcedure, CommandText = "InsertFaction"};
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("FactionId", factionId);
                sqlCommand.Parameters.AddWithValue("Name", factionName);
                sqlCommand.ExecuteScalar();

                sqlCommand = new SqlCommand {Connection = sqlConnection, CommandType = CommandType.StoredProcedure, CommandText = "InsertCharacter_Reputation"};
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("FactionId", factionId);
                sqlCommand.Parameters.AddWithValue("Standing", standing);
                sqlCommand.Parameters.AddWithValue("Value", value);
                sqlCommand.Parameters.AddWithValue("Max", max);
                sqlCommand.ExecuteScalar();
            }
            sqlConnection.Close();
        }

        public void InsertStats(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string) jsonParser.SelectToken("characterId");
            var health = int.Parse(jsonParser.SelectToken("health").ToString());
            var powerType = (string) jsonParser.SelectToken("powerType");
            var power = int.Parse(jsonParser.SelectToken("power").ToString());
            var strength = int.Parse(jsonParser.SelectToken("strength").ToString());
            var agility = (string) jsonParser.SelectToken("agility");
            var intellect = int.Parse(jsonParser.SelectToken("intellect").ToString());
            var spirit = int.Parse(jsonParser.SelectToken("spirit").ToString());
            var attackPower = int.Parse(jsonParser.SelectToken("attackPower").ToString());
            var rangedAttackPower = int.Parse(jsonParser.SelectToken("rangedAttackPower").ToString());
            var mastery = float.Parse(jsonParser.SelectToken("mastery").ToString());
            var masteryRating = int.Parse(jsonParser.SelectToken("masteryRating").ToString());
            var criticalPct = float.Parse(jsonParser.SelectToken("criticalPct").ToString());
            var criticalRating = int.Parse(jsonParser.SelectToken("criticalRating").ToString());
            var hitPct = float.Parse(jsonParser.SelectToken("hitPct").ToString());
            var hitRating = int.Parse(jsonParser.SelectToken("hitRating").ToString());
            var hasteRating = int.Parse(jsonParser.SelectToken("hasteRating").ToString());
            var expertiseRating = int.Parse(jsonParser.SelectToken("expertiseRating").ToString());
            var spellPower = int.Parse(jsonParser.SelectToken("spellPower").ToString());
            var spellPenetration = int.Parse(jsonParser.SelectToken("spellPenetration").ToString());
            var spellCriticalPct = float.Parse(jsonParser.SelectToken("spellCriticalPct").ToString());
            var spellCriticalRating = int.Parse(jsonParser.SelectToken("spellCriticalRating").ToString());
            var spellHitPct = float.Parse(jsonParser.SelectToken("spellHitPct").ToString());
            var spellHitRating = int.Parse(jsonParser.SelectToken("spellHitRating").ToString());
            var mp5 = int.Parse(jsonParser.SelectToken("mp5").ToString());
            var mp5Combat = int.Parse(jsonParser.SelectToken("mp5Combat").ToString());
            var armor = int.Parse(jsonParser.SelectToken("armor").ToString());
            var dodgePct = float.Parse(jsonParser.SelectToken("dodgePct").ToString());
            var dodgeRating = int.Parse(jsonParser.SelectToken("dodgeRating").ToString());
            var parryPct = float.Parse(jsonParser.SelectToken("parryPct").ToString());
            var parryRating = int.Parse(jsonParser.SelectToken("parryRating").ToString());
            var blockPct = float.Parse(jsonParser.SelectToken("blockPct").ToString());
            var blockRating = int.Parse(jsonParser.SelectToken("blockRating").ToString());
            var resillience = int.Parse(jsonParser.SelectToken("resillience").ToString());
            var mainHandDmgMin = int.Parse(jsonParser.SelectToken("mainHandDmgMin").ToString());
            var mainHandDmgMax = int.Parse(jsonParser.SelectToken("mainHandDmgMax").ToString());
            var mainHandSpeed = float.Parse(jsonParser.SelectToken("mainHandSpeed").ToString());
            var mainHandDps = float.Parse(jsonParser.SelectToken("mainHandDps").ToString());
            var mainHandExpertise = int.Parse(jsonParser.SelectToken("mainHandExpertise").ToString());
            var offHandDmgMin = int.Parse(jsonParser.SelectToken("offHandDmgMin").ToString());
            var offHandDmgMax = int.Parse(jsonParser.SelectToken("offHandDmgMax").ToString());
            var offHandSpeed = float.Parse(jsonParser.SelectToken("offHandSpeed").ToString());
            var offHandDps = float.Parse(jsonParser.SelectToken("offHandDps").ToString());
            var offHandExpertise = int.Parse(jsonParser.SelectToken("offHandExpertise").ToString());
            var rangedDmgMin = int.Parse(jsonParser.SelectToken("rangedDmgMin").ToString());
            var rangedDmgMax = int.Parse(jsonParser.SelectToken("rangedDmgMax").ToString());
            var rangedSpeed = float.Parse(jsonParser.SelectToken("rangedSpeed").ToString());
            var rangedDps =float.Parse(jsonParser.SelectToken("rangedDps").ToString());
            var rangedCritPct = float.Parse(jsonParser.SelectToken("rangedCritPct").ToString());
            var rangedCritRating = int.Parse(jsonParser.SelectToken("rangedCritRating").ToString());
            var rangedHitPct = float.Parse(jsonParser.SelectToken("rangedHitPct").ToString());
            var rangedHitRating = int.Parse(jsonParser.SelectToken("rangedHitRating").ToString());
            var stamina = int.Parse(jsonParser.SelectToken("stamina").ToString());

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlCommand = new SqlCommand {Connection = sqlConnection, CommandType = CommandType.StoredProcedure, CommandText = "InsertCharacterStats"};
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
            sqlCommand.Parameters.AddWithValue("Health", health);
            sqlCommand.Parameters.AddWithValue("PowerType", powerType);
            sqlCommand.Parameters.AddWithValue("Power", power);
            sqlCommand.Parameters.AddWithValue("Strength", strength);
            sqlCommand.Parameters.AddWithValue("Agility", agility);
            sqlCommand.Parameters.AddWithValue("Intellect", intellect);
            sqlCommand.Parameters.AddWithValue("Spirit", spirit);
            sqlCommand.Parameters.AddWithValue("AttackPower", attackPower);
            sqlCommand.Parameters.AddWithValue("RangedAttackPower", rangedAttackPower);
            sqlCommand.Parameters.AddWithValue("Mastery", mastery);
            sqlCommand.Parameters.AddWithValue("MasteryRating", masteryRating);
            sqlCommand.Parameters.AddWithValue("CriticalPct", criticalPct);
            sqlCommand.Parameters.AddWithValue("CriticalRating", criticalRating);
            sqlCommand.Parameters.AddWithValue("HitPct", hitPct);
            sqlCommand.Parameters.AddWithValue("HitRating", hitRating);
            sqlCommand.Parameters.AddWithValue("HasteRating", hasteRating);
            sqlCommand.Parameters.AddWithValue("ExpertiseRating", expertiseRating);
            sqlCommand.Parameters.AddWithValue("SpellPower", spellPower);
            sqlCommand.Parameters.AddWithValue("SpellPenetration", spellPenetration);
            sqlCommand.Parameters.AddWithValue("SpellCriticalPct", spellCriticalPct);
            sqlCommand.Parameters.AddWithValue("SpellCriticalRating", spellCriticalRating);
            sqlCommand.Parameters.AddWithValue("SpellHitPct", spellHitPct);
            sqlCommand.Parameters.AddWithValue("SpellHitRating", spellHitRating);
            sqlCommand.Parameters.AddWithValue("MP5", mp5);
            sqlCommand.Parameters.AddWithValue("MP5Combat", mp5Combat);
            sqlCommand.Parameters.AddWithValue("Armor", armor);
            sqlCommand.Parameters.AddWithValue("DodgePct", dodgePct);
            sqlCommand.Parameters.AddWithValue("DodgeRating", dodgeRating);
            sqlCommand.Parameters.AddWithValue("ParryPct", parryPct);
            sqlCommand.Parameters.AddWithValue("ParryRating", parryRating);
            sqlCommand.Parameters.AddWithValue("BlockPct", blockPct);
            sqlCommand.Parameters.AddWithValue("BlockRating", blockRating);
            sqlCommand.Parameters.AddWithValue("Resillience", resillience);
            sqlCommand.Parameters.AddWithValue("MainHandDmgMin", mainHandDmgMin);
            sqlCommand.Parameters.AddWithValue("MainHandDmgMax", mainHandDmgMax);
            sqlCommand.Parameters.AddWithValue("MainHandSpeed", mainHandSpeed);
            sqlCommand.Parameters.AddWithValue("MainHandDps", mainHandDps);
            sqlCommand.Parameters.AddWithValue("MainHandExpertise", mainHandExpertise);
            sqlCommand.Parameters.AddWithValue("OffHandDmgMin", offHandDmgMin);
            sqlCommand.Parameters.AddWithValue("OffHandDmgMax", offHandDmgMax);
            sqlCommand.Parameters.AddWithValue("OffHandSpeed", offHandSpeed);
            sqlCommand.Parameters.AddWithValue("OffHandDps", offHandDps);
            sqlCommand.Parameters.AddWithValue("OffHandExpertise", offHandExpertise);
            sqlCommand.Parameters.AddWithValue("RangedDmgMin", rangedDmgMin);
            sqlCommand.Parameters.AddWithValue("RangedDmgMax", rangedDmgMax);
            sqlCommand.Parameters.AddWithValue("RangedSpeed", rangedSpeed);
            sqlCommand.Parameters.AddWithValue("RangedDps", rangedDps);
            sqlCommand.Parameters.AddWithValue("RangedCritPct", rangedCritPct);
            sqlCommand.Parameters.AddWithValue("RangedCritRating", rangedCritRating);
            sqlCommand.Parameters.AddWithValue("RangedHitPct", rangedHitPct);
            sqlCommand.Parameters.AddWithValue("RangedHitRating", rangedHitRating);
            sqlCommand.Parameters.AddWithValue("Stamina", stamina);

            sqlCommand.ExecuteScalar();
            sqlConnection.Close();
        }

        public void InsertGlyphs(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));
            var numberOfGlyphs = int.Parse(jsonParser.SelectToken("numberOfGlyphs").ToString());

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            for (int i = 0; i < numberOfGlyphs; i++)
            {
                var glyphId = (string)jsonParser.SelectToken("glyphs[" + i + "].glyphId");
                var itemId = (string)jsonParser.SelectToken("glyphs[" + i + "].itemId");
                var icon = (string)jsonParser.SelectToken("glyphs[" + i + "].icon");
                var name = (string)jsonParser.SelectToken("glyphs[" + i + "].name");

                var sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = "InsertGlyph";
                sqlCommand.Parameters.AddWithValue("GlyphId", glyphId);
                sqlCommand.Parameters.AddWithValue("ItemId", itemId);
                sqlCommand.Parameters.AddWithValue("Icon", icon);
                sqlCommand.Parameters.AddWithValue("Name", name);
                sqlCommand.ExecuteScalar();
            }

            sqlConnection.Close();
        }

        public void InsertTalents(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));
            var characterId = (string)jsonParser.SelectToken("characterId");
            var numberOfTalents = int.Parse(jsonParser.SelectToken("numberOfTalents").ToString());

            DeleteTalents(characterId);

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            for (int i = 0; i < numberOfTalents; i++)
            {
                var build = (string) jsonParser.SelectToken("talents[" + i + "].build");
                var icon = (string)jsonParser.SelectToken("talents[" + i + "].icon");
                var name = (string)jsonParser.SelectToken("talents[" + i + "].name");
                var selected = (string)jsonParser.SelectToken("talents[" + i + "].selected");
                var primeGlyph = (string)jsonParser.SelectToken("talents[" + i + "].primeGlyph");
                var majorGlyph = (string)jsonParser.SelectToken("talents[" + i + "].majorGlyph");
                var minorGlyph = (string)jsonParser.SelectToken("talents[" + i + "].minorGlyph");

                var sqlCommand = new SqlCommand {Connection = sqlConnection, CommandType = CommandType.StoredProcedure, CommandText = "InsertCharacterTalent"};
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("Build", build);
                sqlCommand.Parameters.AddWithValue("Icon", icon);
                sqlCommand.Parameters.AddWithValue("Name", name);
                sqlCommand.Parameters.AddWithValue("Selected", selected);
                sqlCommand.Parameters.AddWithValue("PrimeGlyph", primeGlyph);
                sqlCommand.Parameters.AddWithValue("MajorGlyph", majorGlyph);
                sqlCommand.Parameters.AddWithValue("MinorGlyph", minorGlyph);
                sqlCommand.ExecuteScalar();
            }
            sqlConnection.Close();
        }

        public void InsertTitles(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string)jsonParser.SelectToken("characterId");
            var numberOfTitles = int.Parse(jsonParser.SelectToken("numberOfTitles").ToString());

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            for (int i = 0; i < numberOfTitles; i++)
            {
                var titleId = (string) jsonParser.SelectToken("titles[" + i + "].titleId");
                var name = (string)jsonParser.SelectToken("titles[" + i + "].name");
                var selected = (string)jsonParser.SelectToken("titles[" + i + "].selected");

                var sqlCommand = new SqlCommand {Connection = sqlConnection, CommandType = CommandType.StoredProcedure, CommandText = "InsertTitle"};
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("TitleId", titleId);
                sqlCommand.Parameters.AddWithValue("Name", name);
                sqlCommand.ExecuteScalar();

                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = "InsertCharacter_Title";
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("TitleId", titleId);
                sqlCommand.Parameters.AddWithValue("Selected", selected);
                sqlCommand.ExecuteScalar();
            }
            sqlConnection.Close();
        }

        public string InsertGuild(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var guildName = (string)jsonParser.SelectToken("guildName");
            var realmId = (string)jsonParser.SelectToken("realmId");
            var level = (string)jsonParser.SelectToken("level");
            var members = (string)jsonParser.SelectToken("members");
            var side = (string)jsonParser.SelectToken("side");
            var achievementPoints = (string)jsonParser.SelectToken("achievementPoints");

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlCommand = new SqlCommand {Connection = sqlConnection, CommandType = CommandType.StoredProcedure, CommandText = "InsertGuild"};
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.AddWithValue("Name", guildName);
            sqlCommand.Parameters.AddWithValue("RealmId", realmId);
            sqlCommand.Parameters.AddWithValue("Level", level);
            sqlCommand.Parameters.AddWithValue("Members", members);
            sqlCommand.Parameters.AddWithValue("Side", side);
            sqlCommand.Parameters.AddWithValue("AchievementPoints", achievementPoints);
            string guildId = sqlCommand.ExecuteScalar().ToString();

            sqlConnection.Close();

            return guildId;
        }

        public void InsertCharacter(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterName = (string)jsonParser.SelectToken("name");
            var level = int.Parse(jsonParser.SelectToken("level").ToString());
            var classId = int.Parse(jsonParser.SelectToken("class").ToString());
            var race = int.Parse(jsonParser.SelectToken("race").ToString());
            var gender = (string)jsonParser.SelectToken("gender");
            var achievementPoints = int.Parse(jsonParser.SelectToken("achievementPoints").ToString());
            var thumbnail = (string)jsonParser.SelectToken("thumbnail");
            var totalHonorableKills = int.Parse(jsonParser.SelectToken("totalHonorableKills").ToString());
            var personalBattlegroundRating = int.Parse(jsonParser.SelectToken("personalBattlegroundRating").ToString());
            var guildId = int.Parse(jsonParser.SelectToken("guildId").ToString());
            var realmId = int.Parse(jsonParser.SelectToken("realmId").ToString());
            var sideId = int.Parse(jsonParser.SelectToken("sideId").ToString());
            var lastModified = (string)jsonParser.SelectToken("lastModified");

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlCommand = new SqlCommand { CommandType = CommandType.StoredProcedure, Connection = sqlConnection, CommandText = "InsertCharacter" };
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.AddWithValue("Name", characterName);
            sqlCommand.Parameters.AddWithValue("Level", level);
            sqlCommand.Parameters.AddWithValue("Class", classId);
            sqlCommand.Parameters.AddWithValue("Race", race);
            sqlCommand.Parameters.AddWithValue("Gender", gender);
            sqlCommand.Parameters.AddWithValue("AchievementPoints", achievementPoints);
            sqlCommand.Parameters.AddWithValue("Thumbnail", thumbnail);
            sqlCommand.Parameters.AddWithValue("TotalHonorableKills", totalHonorableKills);
            sqlCommand.Parameters.AddWithValue("PersonalBattlegroundRating", personalBattlegroundRating);
            if (guildId==0)
            {
                sqlCommand.Parameters.AddWithValue("GuildId", DBNull.Value);
            }
            else
            {
                sqlCommand.Parameters.AddWithValue("GuildId", guildId);
            }
            sqlCommand.Parameters.AddWithValue("RealmId", realmId);
            sqlCommand.Parameters.AddWithValue("SideId", sideId);
            sqlCommand.Parameters.AddWithValue("LastModified", lastModified);
            sqlCommand.ExecuteScalar();

            sqlConnection.Close();
        }

        #endregion

        private void DeleteTalents(string characterId)
        {
            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlDeleteCommand = new SqlCommand { Connection = sqlConnection };
            sqlDeleteCommand.CommandType = CommandType.Text;
            sqlDeleteCommand.CommandText = "DELETE FROM CharacterTalent WHERE CharacterId='" + characterId + "'";
            sqlDeleteCommand.ExecuteNonQuery();

            sqlConnection.Close();
        }

        private void DeleteTitles(string jsonQuery)
        {
            JObject jsonParser = JObject.Parse(Utilities.Misc.Decompress(jsonQuery));

            var characterId = (string)jsonParser.SelectToken("characterId");

            var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString);
            sqlConnection.Open();

            var sqlDeleteCommand = new SqlCommand { Connection = sqlConnection };
            sqlDeleteCommand.CommandType = CommandType.Text;
            sqlDeleteCommand.CommandText = "DELETE FROM Character_Title WHERE CharacterId='" + characterId + "'";
            sqlDeleteCommand.ExecuteNonQuery();

            sqlConnection.Close();
        }

        private static void InsertItem(JObject jsonParser, string itemSlot, SqlConnection sqlConnection)
        {
            if (jsonParser.SelectToken(itemSlot + ".itemId") != null)
            {
                var itemId = int.Parse(jsonParser.SelectToken(itemSlot + ".itemId").ToString());
                var icon = (string)jsonParser.SelectToken(itemSlot + ".icon");
                var name = (string)jsonParser.SelectToken(itemSlot + ".name");
                var quality = (string)jsonParser.SelectToken(itemSlot + ".quality");

                var sqlCommand = new SqlCommand { CommandType = CommandType.StoredProcedure, Connection = sqlConnection, CommandText = "InsertItem" };
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("ItemId", itemId);
                sqlCommand.Parameters.AddWithValue("Icon", icon);
                sqlCommand.Parameters.AddWithValue("Name", name);
                sqlCommand.Parameters.AddWithValue("Quality", quality);
                sqlCommand.ExecuteScalar();
            }
        }

        private static void InsertItemEnhancement(string characterId, JObject jsonParser, string itemSlot, SqlConnection sqlConnection)
        {
            if (jsonParser.SelectToken(itemSlot + ".itemId") != null)
            {
                var itemId = (string)jsonParser.SelectToken(itemSlot + ".itemId");
                var enchant = (string)jsonParser.SelectToken(itemSlot + ".enchant");
                var extraSocket = (string)jsonParser.SelectToken(itemSlot + ".extraSocket");
                var gem0 = (string)jsonParser.SelectToken(itemSlot + ".gem0");
                var gem1 = (string)jsonParser.SelectToken(itemSlot + ".gem1");
                var gem2 = (string)jsonParser.SelectToken(itemSlot + ".gem2");
                var reforge = (string)jsonParser.SelectToken(itemSlot + ".reforge");
                var seed = (string)jsonParser.SelectToken(itemSlot + ".seed");
                var set = (string)jsonParser.SelectToken(itemSlot + ".set");
                var suffix = (string)jsonParser.SelectToken(itemSlot + ".suffix");

                var sqlCommand = new SqlCommand { CommandType = CommandType.StoredProcedure, Connection = sqlConnection, CommandText = "InsertCharacter_ItemEnhancement" };
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("ItemId", itemId);
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("Enchant", enchant);
                sqlCommand.Parameters.AddWithValue("ExtraSocket", extraSocket);
                sqlCommand.Parameters.AddWithValue("Gem0", gem0);
                sqlCommand.Parameters.AddWithValue("Gem1", gem1);
                sqlCommand.Parameters.AddWithValue("Gem2", gem2);
                sqlCommand.Parameters.AddWithValue("Reforge", reforge);
                sqlCommand.Parameters.AddWithValue("Seed", seed);
                sqlCommand.Parameters.AddWithValue("Set", set);
                sqlCommand.Parameters.AddWithValue("Suffix", suffix);
                sqlCommand.ExecuteScalar();
            }
        }
    }
}